package com.smartleanx.module.hrm.controller.admin.employee;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.security.core.annotations.PreAuthenticated;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.employee.train.EmployeeTrainingExperienceRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.train.EmployeeTrainingExperienceSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeTrainingExperienceDO;
import com.smartleanx.module.hrm.service.employee.EmployeeTrainingExperienceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "员工PC端 - 员工培训经历")
@RestController
@RequestMapping("/hrm/pc-employee-training-experience")
@Validated
public class PCEmployeeTrainingExperienceController {

    @Resource
    private EmployeeTrainingExperienceService employeeTrainingExperienceService;

    @PostMapping("/create")
    @Operation(summary = "创建员工培训经历")
    @PreAuthenticated
    public CommonResult<Long> createEmployeeTrainingExperience(@Valid @RequestBody EmployeeTrainingExperienceSaveReqVO createReqVO) {
        createReqVO.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        return success(employeeTrainingExperienceService.createEmployeeTrainingExperience(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工培训经历")
    @PreAuthenticated
    public CommonResult<Boolean> updateEmployeeTrainingExperience(@Valid @RequestBody EmployeeTrainingExperienceSaveReqVO updateReqVO) {
        updateReqVO.setEmployeeId(EmployeeContextHolder.getEmployeeId());
        employeeTrainingExperienceService.updateEmployeeTrainingExperience(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工培训经历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteEmployeeTrainingExperience(@RequestParam("id") Long id) {
        employeeTrainingExperienceService.deleteEmployeeTrainingExperience(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工培训经历")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<EmployeeTrainingExperienceRespVO> getEmployeeTrainingExperience(@RequestParam("id") Long id) {
        EmployeeTrainingExperienceDO employeeTrainingExperience = employeeTrainingExperienceService.getEmployeeTrainingExperience(id);
        return success(BeanUtils.toBean(employeeTrainingExperience, EmployeeTrainingExperienceRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "根据员工id获得培训经历列表")
    @PreAuthenticated
    public CommonResult<List<EmployeeTrainingExperienceRespVO>> getEmployeeTrainingExperienceList() {
        Long employeeId = EmployeeContextHolder.getEmployeeId();
        List<EmployeeTrainingExperienceDO> trainingExperienceList = employeeTrainingExperienceService.getEmployeeTrainingExperienceList(employeeId);
        return success(BeanUtils.toBean(trainingExperienceList, EmployeeTrainingExperienceRespVO.class));
    }

}
